package com.uinnova.product.eam.service.impl;

import com.binary.core.lang.StringUtils;
import com.uinnova.product.eam.service.EADixUserSvc;
import com.uino.bean.cmdb.business.ImportSheetMessage;
import com.uino.bean.permission.base.SysOrg;
import com.uino.bean.permission.base.SysUser;
import com.uino.bean.permission.base.SysUserOrgRlt;
import com.uino.bean.permission.business.UserInfo;
import com.uino.dao.BaseConst;
import com.uino.dao.permission.ESOrgSvc;
import com.uino.dao.permission.rlt.ESUserOrgRltSvc;
import com.uino.dao.util.ESUtil;
import com.uino.service.permission.microservice.IUserSvc;
import com.uino.util.sys.SysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class EADixUserSvcImpl implements EADixUserSvc {

    @Autowired
    private ESOrgSvc esOrgSvc;

    @Autowired
    private IUserSvc userSvc;

    @Autowired
    private ESUserOrgRltSvc esUserOrgRltSvc;

    public Integer saveBatchOrg(List<SysOrg> orgList) {
        SysUser currentUserInfo = SysUtil.getCurrentUserInfo();
        Long domainId = currentUserInfo.getDomainId();
        if (!CollectionUtils.isEmpty(orgList)) {
            for (SysOrg sysOrg : orgList) {
                sysOrg.setDomainId(domainId);
                if (StringUtils.isBlank(sysOrg.getOrgCode())) {
                    sysOrg.setOrgCode(String.valueOf(ESUtil.getUUID()));
                }
            }
            return esOrgSvc.saveOrUpdateBatch(orgList);
        }
        return 0;
    }

    @Override
    public ImportSheetMessage saveBatchUser(List<UserInfo> userInfos) {
        return userSvc.syncUserBatch(BaseConst.DEFAULT_DOMAIN_ID,userInfos);
    }

    @Override
    public Integer saveBatchOrgUserRlt(List<SysUserOrgRlt> rltList) {
        return esUserOrgRltSvc.saveOrUpdateBatch(rltList);
    }


}
